
//控制器
define(["jquery","core","manage.service","angular.util","angular"],function($,core,service,util)
{
	var getIncludePath=function(path)
	{
		if(core.util.isBlank(path))
			return null;
		
		if(path.substring(0,1)!="/")
			path="/"+path;
		
		path=core.System.MANAGE_INCLUDE_PATH+path;
		return path;	
	};
	
	var baseDialog=function(option,callback)
	{
		option.path=getIncludePath(option.path);
		util.angular.dialog.baseDialog(option,callback);
	};
	
	var baseCreate=function(option)
	{
		option.path=getIncludePath(option.path);
		return util.angular.dialog.baseCreate(option);
	};
	
	var baseEdit=function(option)
	{
		option.path=getIncludePath(option.path);
		return util.angular.dialog.baseEdit(option);
	};
	
	var baseRemove=function(option)
	{
		option.path=getIncludePath(option.path);
		return util.angular.dialog.baseRemove(option);
	};
	
	var controllers=angular.module("manage.controllers",[]);
	
	//管理首页控制器
	controllers.controller("ManageIndexController",["$scope",function($scope)
	{
		util.changeTitle("后台管理");
		//获得菜单
		var findMenu=function()
		{
			service.menu.findMenu(function(data)
			{
				if(core.util.isBlank(data.data))
				{
					core.ui.simpleDialog(core.message.message.UNLAWFUL_OPERATION,function()
					{
						util.changeLocation();
					});
					return;
				}
				
				for(var i=0;i<data.data.length;i++)
				{
					if(data.data[i].children.length==0)
						data.data.splice(i,1);
				}
				$scope.menus=data.data;
				forword(data.main);
				$scope.$apply();
			});
		};

		//跳转
		var forword=function(menu)
		{
			if(menu==null)
				return;
			
			if(core.util.isBlank(menu.include_path))
				menu.include_path=getIncludePath(menu.path);
			
			$scope.mainMenu=menu;
		};
		
		findMenu();
		$scope.forword=forword;
	}]);
	
	//显示模块控制器
	controllers.controller("ModuleShowController",["$scope",function($scope)
	{
		var moduleTypeAndRoleType={module:null,role:null};
		
		//获得模块类型
		var findModuleTypeAndRoleType=function()
		{
			service.module.findType(function(moduleType)
			{
				service.user.role.findType(function(roleType)
				{
					if(core.util.isBlank(moduleType) || core.util.isBlank(roleType))
					{
						core.ui.simpleDialog(core.message.message.ERROR_DATA_ERROR);
						return;
					}
					
					moduleTypeAndRoleType.module=moduleType[0].id;
					moduleTypeAndRoleType.role=roleType[0].id;
					$scope.moduleTypes=moduleType;
					$scope.roleTypes=roleType;
					$scope.$apply();
					updateView();
				});
			});
		};
		
		//更新视图数据
		var updateView=function()
		{
			service.module.findByType(moduleTypeAndRoleType.module,function(modules)
			{
				if(core.util.isBlank(modules))
				{
					core.ui.simpleDialog(core.message.message.ERROR_DATA_ERROR);
					return;
				}
				
				service.user.role.findByRoleAndType(moduleTypeAndRoleType.role,moduleTypeAndRoleType.module,function(roleModules)
				{
					for(var i=0;i<modules.length;i++)
					{
						if(core.util.isBlank(roleModules))
							modules[i].select=false;
						else
						{
							for(var j=0,len=roleModules.length;j<len;j++)
							{
								if(roleModules[j].id==modules[i].id)
								{
									modules[i].select=true;
									break;
								}
								
								if(j==len-1)
									modules[i].select=false;
							}
						}
					}

					$scope.modules=modules;
					$scope.$apply();
				});
			});
		};
		
		//更新数据
		var update=function()
		{
			var modules=$scope.modules;
			var arg=[];
			for(var i=0,len=modules.length;i<len;i++)
			{
				var module=modules[i];
				if(module.select==true)
					arg.push(module.id);
			}
			
			service.user.role.updateModuleByRole(moduleTypeAndRoleType.role,moduleTypeAndRoleType.module,arg,function(result)
			{
				core.ui.simpleDialog(result.message);
			});
		};
		
		$scope.moduleTypeAndRoleType=moduleTypeAndRoleType;
		$scope.updateView=updateView;
		$scope.update=update;
		findModuleTypeAndRoleType();
	}]);
	
	//角色类型控制器
	controllers.controller("RoleTypeShowController",["$scope",function($scope)
	{
		service.user.role.findType(function(roles)
		{
			if(core.util.isBlank(roles))
			{
				core.ui.simpleDialog(core.message.message.ERROR_DATA_ERROR);
				return;
			}
			$scope.roles=roles;
			$scope.$apply();
		});
		
		//设置默认
		$scope.setDefault=function(role)
		{
			service.user.role.setDefault(role.id,function(result)
			{
				core.ui.simpleDialog(result.message);
			});
		};
		
		$scope.create=function()
		{
			baseCreate
			({
				title:"创建角色",
				path:"/role_type_create.html",
				moduleName:"roleTypeCreate",
				controllerName:"RoleTypeCreateController",
				controller:
				{
					func:service.user.role.create,
					$scope:$scope,
					item:"role",
					items:$scope.roles
				}
			});
		};
		
		$scope.edit=function(roleType)
		{
			baseEdit
			({
				title:"编辑角色",
				path:"/role_type_edit.html",
				moduleName:"roleTypeEdit",
				controllerName:"RoleTypeEditController",
				controller:
				{
					func:service.user.role.update,
					$scope:$scope,
					source:roleType,
					item:"role",
					items:$scope.roles
				}
			});
		};
		
		$scope.remove=function(roleType)
		{
			baseDialog
			({
				title:"角色用户转移",
				path:"/role_type_user_move.html",
				controllerName:"RoleTypeUserMoveController"
			},function($removeScope,dialog)
			{
				service.user.role.findType(function(roles)
				{
					util.baseRemove(roles,roleType);
					$removeScope.roles=roles;
					$removeScope.$apply();
				});
				
				$removeScope.remove=function()
				{
					service.user.role.remove(roleType.id,$removeScope.target,function(result)
					{
						core.ui.simpleDialog(result.message,function()
						{
							if(result.success)
							{
								dialog.close();
								if(util.baseRemove($scope.roles,roleType))
									$scope.$apply();
							}
						});
					});
				};
				
				$removeScope.cancel=function()
				{
					dialog.close();
				};
			});
		};
		
	}]);
	
	//配置控制器
	controllers.controller("ConfigShowController",["$scope",function($scope)
	{
		var find=function()
		{
			service.config.find(function(data)
			{
				$scope.configs=data;
				$scope.$apply();
			});
		};
		$scope.edit=function(config)
		{
			baseEdit
			({
				title:"编辑配置",
				path:"/config_edit.html",
				moduleName:"configEdit",
				controllerName:"ConfigEditController",
				controller:
				{
					func:service.config.update,
					$scope:$scope,
					source:config,
					item:"config",
					items:$scope.configs
				}
			});
		};
		
		find();
	}]);
	
	//用户信息条控制器
	controllers.controller("UserBarShowController",["$scope",function($scope)
	{
		var updateView=function()
		{
			service.user.bar.role.findByRole($scope.roleType,function(data)
			{
				var bars=$scope.bars.all;
				for(var i=0;i<bars.length;i++)
				{
					if(core.util.isBlank(data))
						bars[i].select=false;
					else
					{
						for(var j=0;j<data.length;j++)
						{
							if(data[j].id==bars[i].id)
							{
								bars[i].select=true;
								break;
							}
								
							if(j==data.length-1)
								bars[i].select=false;
						}
					}
				}
				
				$scope.$apply();
			});
		};
		
		var find=function()
		{
			service.user.bar.find(function(bars)
			{
				var roots=[{id:0,text:"根节点"}];
				
				if(!core.util.isBlank(bars) && !core.util.isBlank(bars.root))
				{
					for(var i=0;i<bars.root.length;i++)
						roots.push(bars.root[i]);
				}
				$scope.roots=roots;
				$scope.bars=bars;
				$scope.$apply();
			});
		};
		
		service.user.role.findType(function(roleTypes)
		{
			if(core.util.isBlank(roleTypes))
			{
				core.ui.simpleDialog(core.message.message.ERROR_DATA_ERROR);
				return;
			}
			
			$scope.roleType=roleTypes[0].id;
			$scope.roleTypes=roleTypes;
			updateView();
			$scope.$apply();
		});
		
		$scope.updateBarRole=function()
		{
			var bars=$scope.bars.all;
			var arg=[];
			for(var i=0,len=bars.length;i<len;i++)
			{
				var bar=bars[i];
				if(bar.select==true)
					arg.push(bar.id);
			}
			
			service.user.bar.role.update($scope.roleType,arg,function(result)
			{
				core.ui.simpleDialog(result.message);
			});
		};
		
		find();
		$scope.updateView=updateView;
		
		//创建信息条控制器
		$scope.create=function()
		{
			baseCreate
			({
				title:"创建信息条",
				path:"/user_bar_create.html",
				moduleName:"userBarCreate",
				controllerName:"UserBarCreateController",
				controller:
				{
					func:service.user.bar.create,
					$scope:$scope,
					item:"bar",
					items:$scope.bars,
					controller:function($createScope)
					{
						$createScope.roots=$scope.roots;
					},
					pushHandler:function($createScope,option,result)
					{
						find();
					}
				}
			});
		};
		
		$scope.edit=function(bar)
		{
			baseEdit
			({
				title:"编辑信息条",
				path:"/user_bar_edit.html",
				moduleName:"userBarEdit",
				controllerName:"UserBarEditController",
				controller:
				{
					func:service.user.bar.update,
					$scope:$scope,
					source:bar,
					item:"bar",
					items:$scope.bars,
					controller:function($editScope)
					{
						$editScope.roots=$scope.roots;
					},
					updateHandler:function($editScope,option,result,source)
					{
						find();
					}
				}
			});
		};
		
		$scope.remove=function(bar)
		{
			baseRemove
			({
				func:service.user.bar.remove,
				$scope:$scope,
				item:bar,
				items:$scope.bars,
				removeHandler:function(option)
				{
					find();
				}
			});
		};
	}]);
	
	//非法字符控制器
	controllers.controller("IllegalCharShowController",["$scope",function($scope)
	{
		service.illegalChar.find(function(data)
		{
			$scope.illegalChars=data;
			$scope.$apply();
		});

		$scope.create=function()
		{
			baseCreate
			({
				title:"创建非法字符",
				path:"/illegal_char_create.html",
				moduleName:"illegalCharCreate",
				controllerName:"IllegalCharCreateController",
				controller:
				{
					func:service.illegalChar.create,
					$scope:$scope,
					item:"illegalChar",
					items:$scope.illegalChars
				}
			});
		};
		
		$scope.edit=function(illegalChar)
		{
			baseEdit
			({
				title:"编辑非法字符",
				path:"/illegal_char_edit.html",
				moduleName:"illegalCharEdit",
				controllerName:"IllegalCharEditController",
				controller:
				{
					func:service.illegalChar.update,
					$scope:$scope,
					source:illegalChar,
					item:"illegalChar",
					items:$scope.illegalChars
				}
			});
		};
		
		$scope.remove=function(illegalChar)
		{
			baseRemove
			({
				func:service.illegalChar.remove,
				$scope:$scope,
				item:illegalChar,
				items:$scope.illegalChars
			});
		};
	}]);
	
	//API显示
	controllers.controller("OpenApiShowController",["$scope",function($scope)
	{
		var find=function()
		{
			service.openApi.find(function(data)
			{
				var roots=[{id:0,name:"根节点"}];
				
				if(!core.util.isBlank(data) && !core.util.isBlank(data.root))
				{
					for(var i=0;i<data.root.length;i++)
						roots.push(data.root[i]);
				}
				$scope.roots=roots;
				$scope.apis=data;
				$scope.$apply();
			});
		};
		
		find();
		$scope.create=function()
		{
			baseCreate
			({
				title:"创建Api",
				path:"/open_api_create.html",
				width:850,
				moduleName:"oepnApiCreate",
				controllerName:"OpenApiCreateController",
				controller:
				{
					func:service.openApi.create,
					$scope:$scope,
					item:"openApi",
					items:$scope.apis,
					controller:function($createScope)
					{
						$createScope.roots=$scope.roots;
					},
					pushHandler:function()
					{
						find();
					}
				}
			});
		};
		
		$scope.edit=function(openApi)
		{
			baseEdit
			({
				title:"编辑Api",
				path:"/open_api_edit.html",
				width:850,
				moduleName:"openApiEdit",
				controllerName:"OpenApiEditController",
				controller:
				{
					func:service.openApi.update,
					$scope:$scope,
					source:openApi,
					item:"openApi",
					items:$scope.apis,
					controller:function($editScope)
					{
						$editScope.roots=$scope.roots;
					},
					updateHandler:function()
					{
						find();
					}
				}
			});
		};
		
		$scope.remove=function(openApi)
		{
			baseRemove
			({
				func:service.openApi.remove,
				$scope:$scope,
				item:openApi,
				items:$scope.apis,
				removeHandler:function()
				{
					find();
				}
			});
		};
	}]);
	
	//软件审核控制器
	controllers.controller("SoftwareCheckController",["$scope",function($scope)
	{
		var findCheck=function(pn)
		{
			service.software.findCheck(pn,function(data)
			{
				$scope.softwares=data;
				$scope.$apply();
			});
		};
		
		$scope.pass=function(software)
		{
			baseRemove
			({
				func:service.software.checkPass,
				$scope:$scope,
				item:software,
				items:$scope.softwares.data,
				message:"是否通过?"
			});
		};
		
		$scope.failure=function(software)
		{
			baseRemove
			({
				func:service.software.checkFailure,
				$scope:$scope,
				item:software,
				items:$scope.softwares.data
			});
		};
		
		$scope.edit=function(software)
		{
			baseEdit
			({
				title:"编辑软件",
				path:"/software_edit.html",
				moduleName:"softwareEdit",
				controllerName:"SoftwareEditController",
				controller:
				{
					func:service.software.update,
					$scope:$scope,
					source:software,
					item:"software",
					items:$scope.softwares.data,
					controller:function($editScope)
					{
						$editScope.changeIcon=function(path)
						{
							$editScope.software.icon=path;
							$editScope.$apply();
						};
					}
				}
			});
		};
		
		$scope.pre=function(software)
		{
			baseDialog
			({
				title:"预览",
				path:"/software_pre.html",
				moduleName:"softwarePre",
				controllerName:"SoftwarePreController"
			},function($preScope)
			{
				$preScope.software=software;
			});
		};
		
		findCheck(1);
		$scope.findCheck=findCheck;
	}]);
	
	//插件控制器
	controllers.controller("PluginShowController",["$scope",function($scope)
	{
		var find=function()
		{
			service.plugin.find(function(data)
			{
				$scope.plugins=data;
				$scope.$apply();
			});	
		};
		
		$scope.remove=function(plugin)
		{
			baseRemove
			({
				func:service.plugin.remove,
				$scope:$scope,
				item:plugin,
				items:$scope.plugins
			});
		};
		
		$scope.setEnabled=function(plugin,enabled)
		{
			var checkDialog=core.ui.simpleDialogEx("是否更新状态?",function()
			{
				service.plugin.setEnabled(plugin.id,enabled,function(result)
				{
					checkDialog.close();
					core.ui.simpleDialog(result.message);
					if(result.success)
						find();
				});
			});
		};
		
		find();
	}]);
	
	//板块管理
	controllers.controller("DiscussForumShowController",["$scope",function($scope)
	{
		$scope.create=function()
		{
			baseCreate
			({
				title:"添加板块",
				path:"/discuss_forum_create.html",
				moduleName:"discussForumCreate",
				controllerName:"DiscussForumCreateController",
				controller:
				{
					func:service.discuss.forum.create,
					controller:function($editScope)
					{
						service.discuss.forum.index(function(data)
						{
							$editScope.indexTypes=data;
							$editScope.$apply();
						});
					},
					$scope:$scope,
					item:"forum",
					items:$scope.forums
				}
			});
		};
		
		$scope.edit=function(forum)
		{
			baseEdit
			({
				title:"编辑板块",
				path:"/discuss_forum_edit.html",
				moduleName:"discussForumEdit",
				controllerName:"DiscussForumEditController",
				controller:
				{
					func:service.discuss.forum.update,
					controller:function($editScope)
					{
						service.discuss.forum.index(function(data)
						{
							$editScope.indexTypes=data;
							$editScope.$apply();
						});
					},
					$scope:$scope,
					source:forum,
					item:"forum",
					items:$scope.forums
				}
			});
		};
		
		service.discuss.forum.find(function(data)
		{
			$scope.forums=data;
			$scope.$apply();
		});
	}]);
	
	//排序管理
	controllers.controller("DiscussOrderShowController",["$scope",function($scope)
	{
		$scope.create=function()
		{
			baseCreate
			({
				title:"添加排序",
				path:"/discuss_order_create.html",
				moduleName:"discussOrderCreate",
				controllerName:"DiscussOrderCreateController",
				controller:
				{
					func:service.discuss.order.create,
					$scope:$scope,
					item:"order",
					items:$scope.orders
				}
			});
		};
		
		$scope.edit=function(order)
		{
			baseEdit
			({
				title:"编辑排序",
				path:"/discuss_order_edit.html",
				moduleName:"discussOrderEdit",
				controllerName:"DiscussOrderEditController",
				controller:
				{
					func:service.discuss.order.update,
					$scope:$scope,
					source:order,
					item:"order",
					items:$scope.orders
				}
			});
		};
		
		$scope.setDefault=function(order)
		{
			service.discuss.order.setDefault(order.id,function(result)
			{
				core.ui.simpleDialog(result.message);
			})
		};
		
		service.discuss.order.find(function(data)
		{
			$scope.orders=data;
			$scope.$apply();
		});
	}]);
	
	//讨论审核
	controllers.controller("DiscussCheckController",["$scope",function($scope)
	{
		var forums=null;
		var findCheck=function(pn)
		{
			service.discuss.findCheck(pn,function(data)
			{
				var handle=function()
				{
					if(!core.util.isBlank(data.data))
					{
						for(var i=0;i<data.data.length;i++)
						{
							for(var j=0;j<forums.length;j++)
							{
								if(data.data[i].forum.id==forums[j].id)
									data.data[i].forum.name=forums[j].name;
							}
						}
					}
					$scope.discuss=data;
					$scope.$apply();
				};
				
				if(core.util.isBlank(forums))
				{
					service.discuss.forum.find(function(forumsData)
					{
						forums=forumsData;
						handle();
					});
				}
				else
					handle();
			});
		};
		
		$scope.pass=function(discuss)
		{
			baseRemove
			({
				func:service.discuss.checkPass,
				$scope:$scope,
				item:discuss,
				items:$scope.discuss.data,
				message:"是否通过?"
			});
		};
		
		$scope.failure=function(discuss)
		{
			baseRemove
			({
				func:service.discuss.checkFailure,
				$scope:$scope,
				item:discuss,
				items:$scope.discuss.data
			});
		};
		
		$scope.pre=function(discuss)
		{
			baseDialog
			({
				title:"预览",
				path:"/discuss_pre.html",
				moduleName:"discussPre",
				controllerName:"DiscussPreController"
			},function($preScope)
			{
				$preScope.discuss=discuss;
			});
		};
		
		findCheck(1);
		$scope.findCheck=findCheck;
	}]);
	
	//上传配置控制器
	controllers.controller("UploadConfigShowController",["$scope",function($scope)
	{
		service.upload.config.find(function(data)
		{
			$scope.configs=data;
			$scope.$apply();
		});
		
		$scope.edit=function(config)
		{
			baseEdit
			({
				title:"编辑配置",
				path:"/upload_config_edit.html",
				moduleName:"uploadConfigEdit",
				controllerName:"UploadConfigEditController",
				controller:
				{
					func:service.upload.config.update,
					$scope:$scope,
					source:config,
					item:"config",
					items:$scope.configs
				}
			});
		};
	}]);
	
	//上传记录控制器
	controllers.controller("UploadRecordShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.upload.record.find(pn,function(data)
			{
				$scope.records=data;
				$scope.$apply();
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//友情链接控制器
	controllers.controller("FriendlinkShowController",["$scope",function($scope)
	{
		service.friendlink.find(function(data)
		{
			$scope.friendlinks=data;
			$scope.$apply();
		});
		
		$scope.create=function()
		{
			baseCreate
			({
				title:"添加友情链接",
				path:"/friendlink_create.html",
				moduleName:"friendlinkCreate",
				controllerName:"FriendlinkCreateController",
				controller:
				{
					func:service.friendlink.create,
					$scope:$scope,
					item:"friendlink",
					items:$scope.friendlinks
				}
			});
		};
		
		$scope.edit=function(friendlink)
		{
			baseEdit
			({
				title:"编辑友情链接",
				path:"/friendlink_edit.html",
				moduleName:"friendEdit",
				controllerName:"FriendlinkEditController",
				controller:
				{
					func:service.friendlink.update,
					$scope:$scope,
					source:friendlink,
					item:"friendlink",
					items:$scope.friendlinks
				}
			});
		};
		
		$scope.remove=function(friendlink)
		{
			baseRemove
			({
				func:service.friendlink.remove,
				$scope:$scope,
				item:friendlink,
				items:$scope.friendlinks
			});
		};
	}]);
	
	//显示已删除的动弹
	controllers.controller("DongtanDeleteShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.dongtan.findDelete(pn,function(data)
			{
				$scope.dongtan=data;
				$scope.$apply();
			});
		};
		
		$scope.recover=function(dongtan)
		{
			baseRemove
			({
				func:service.dongtan.recoverDelete,
				$scope:$scope,
				item:dongtan,
				items:$scope.dongtan.data,
				message:"是否恢复?"
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//显示已删除的讨论
	controllers.controller("DiscussDeleteShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.discuss.findDelete(pn,function(data)
			{
				$scope.discuss=data;
				$scope.$apply();
			});
		};
		
		$scope.recover=function(discuss)
		{
			baseRemove
			({
				func:service.discuss.recoverDelete,
				$scope:$scope,
				item:discuss,
				items:$scope.discuss.data,
				message:"是否恢复?"
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//博客排序管理
	controllers.controller("BlogOrderShowController",["$scope",function($scope)
	{
		service.blog.order.find(function(data)
		{
			$scope.orders=data;
			$scope.$apply();
		});
		
		$scope.create=function()
		{
			baseCreate
			({
				title:"添加排序",
				path:"/blog_order_create.html",
				moduleName:"blogOrderCreate",
				controllerName:"BlogOrderCreateController",
				controller:
				{
					func:service.blog.order.create,
					$scope:$scope,
					item:"order",
					items:$scope.orders
				}
			});
		};
		
		$scope.edit=function(order)
		{
			baseEdit
			({
				title:"编辑排序",
				path:"/blog_order_edit.html",
				moduleName:"blogOrderEdit",
				controllerName:"BlogOrderEditController",
				controller:
				{
					func:service.blog.order.update,
					$scope:$scope,
					source:order,
					item:"order",
					items:$scope.orders
				}
			});
		};
	}]);
	
	//用户菜单控制器
	controllers.controller("UserMenuShowController",["$scope",function($scope)
	{
		var updateView=function()
		{
			service.user.menu.role.findByRole($scope.roleType,function(data)
			{
				var menus=$scope.menus.all;
				for(var i=0;i<menus.length;i++)
				{
					if(core.util.isBlank(data))
						menus[i].select=false;
					else
					{
						for(var j=0;j<data.length;j++)
						{
							if(data[j].id==menus[i].id)
							{
								menus[i].select=true;
								break;
							}
								
							if(j==data.length-1)
								menus[i].select=false;
						}
					}
				}
				
				$scope.$apply();
			});
		};
		
		var find=function()
		{
			service.user.menu.find(function(menus)
			{
				var roots=[{id:0,text:"根节点"}];
				
				if(!core.util.isBlank(menus) && !core.util.isBlank(menus.root))
				{
					for(var i=0;i<menus.root.length;i++)
						roots.push(menus.root[i]);
				}
				$scope.roots=roots;
				$scope.menus=menus;
				$scope.$apply();
			});
		};
		
		service.user.role.findType(function(roleTypes)
		{
			if(core.util.isBlank(roleTypes))
			{
				core.ui.simpleDialog(core.message.message.ERROR_DATA_ERROR);
				return;
			}
			
			$scope.roleType=roleTypes[0].id;
			$scope.roleTypes=roleTypes;
			updateView();
			$scope.$apply();
		});
		
		$scope.updateMenuRole=function()
		{
			var menus=$scope.menus.all;
			var arg=[];
			for(var i=0,len=menus.length;i<len;i++)
			{
				var menu=menus[i];
				if(menu.select==true)
					arg.push(menu.id);
			}
			
			service.user.menu.role.update($scope.roleType,arg,function(result)
			{
				core.ui.simpleDialog(result.message);
			});
		};
		
		find();
		$scope.updateView=updateView;
		
		//创建信息条控制器
		$scope.create=function()
		{
			baseCreate
			({
				title:"创建菜单",
				path:"/user_menu_create.html",
				moduleName:"userMenuCreate",
				controllerName:"UserMenuCreateController",
				controller:
				{
					func:service.user.menu.create,
					$scope:$scope,
					item:"menu",
					items:$scope.menus,
					controller:function($createScope)
					{
						$createScope.roots=$scope.roots;
					},
					pushHandler:function($createScope,option,result)
					{
						find();
					}
				}
			});
		};
		
		$scope.edit=function(menu)
		{
			baseEdit
			({
				title:"编辑菜单",
				path:"/user_menu_edit.html",
				moduleName:"userMenuEdit",
				controllerName:"UserMenuEditController",
				controller:
				{
					func:service.user.menu.update,
					$scope:$scope,
					source:menu,
					item:"menu",
					items:$scope.menus,
					controller:function($editScope)
					{
						$editScope.roots=$scope.roots;
					},
					updateHandler:function($editScope,option,result,source)
					{
						find();
					}
				}
			});
		};
		
		$scope.remove=function(menu)
		{
			baseRemove
			({
				func:service.user.menu.remove,
				$scope:$scope,
				item:menu,
				items:$scope.menus,
				removeHandler:function(option)
				{
					find();
				}
			});
		};
	}]);
	
	//动态类型控制器
	controllers.controller("UserActiveTypeController",["$scope",function($scope)
	{
		service.user.active.type.find(function(data)
		{
			$scope.types=data;
			$scope.$apply();
		});
		
		$scope.edit=function(type)
		{
			baseEdit
			({
				title:"编辑类型",
				path:"/user_active_type_edit.html",
				moduleName:"userActiveTypeEdit",
				controllerName:"UserActiveTypeEditController",
				controller:
				{
					func:service.user.active.type.update,
					$scope:$scope,
					source:type,
					item:"type",
					items:$scope.types
				}
			});
		};
	}]);
	
	//邮件类型控制器
	controllers.controller("UserMailTypeShowController",["$scope",function($scope)
	{
		service.user.mail.type.find(function(data)
		{
			$scope.types=data;
			$scope.$apply();
		});
		
		$scope.edit=function(type)
		{
			baseEdit
			({
				title:"编辑类型",
				path:"/user_mail_type_edit.html",
				moduleName:"userMailTypeEdit",
				controllerName:"UserMailTypeEditController",
				controller:
				{
					func:service.user.mail.type.update,
					$scope:$scope,
					source:type,
					item:"type",
					items:$scope.types
				}
			});
		};
	}]);
	
	//系统邮件控制器
	controllers.controller("UserMailSystemShowController",["$scope",function($scope)
	{
		var role=[{id:0,name:"全部"}];
		service.user.role.findType(function(data)
		{
			for(var i=0;i<data.length;i++)
				role.push(data[i]);
			$scope.role=role;
			$scope.$apply();
		});
		
		var find=function(pn)
		{
			service.user.mail.system.find(pn,function(data)
			{
				$scope.mail=data;
				$scope.$apply();
			});
		};
		
		$scope.create=function()
		{
			baseCreate
			({
				title:"发送系统邮件",
				path:"/user_mail_system_create.html",
				moduleName:"userMailSystemCreate",
				controllerName:"UserMailSystemCreateController",
				controller:
				{
					func:service.user.mail.system.create,
					$scope:$scope,
					item:"mail",
					items:$scope.mail.data,
					controller:function($createScope)
					{
						$createScope.role=role;
					}
				}
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//收藏类型控制器
	controllers.controller("FavoriteTypeShowController",["$scope",function($scope)
	{
		service.favorite.type.find(function(types)
		{
			$scope.types=types;
			$scope.$apply();
		});
		
		$scope.edit=function(type)
		{
			baseEdit
			({
				title:"编辑类型",
				path:"/favorite_type_edit.html",
				moduleName:"favoriteTypeEdit",
				controllerName:"FavoriteTypeEditController",
				controller:
				{
					func:service.favorite.type.update,
					$scope:$scope,
					source:type,
					item:"type",
					items:$scope.types
				}
			});
		};
	}]);
	
	//讨论举报控制器
	controllers.controller("DiscussReportShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.discuss.report.find(pn,function(data)
			{
				$scope.discuss=data;
				$scope.$apply();
			});
		};
		
		$scope.recover=function(discuss)
		{
			baseRemove
			({
				func:service.discuss.recoverDelete,
				$scope:$scope,
				item:discuss,
				items:$scope.discuss.data,
				message:"是否恢复?"
			});
		};
		
		$scope.remove=function(discuss)
		{
			baseRemove
			({
				func:service.discuss.remove,
				$scope:$scope,
				item:discuss,
				items:$scope.discuss.data
			});
		};
		
		$scope.concrete=function(discuss)
		{
			baseDialog
			({
				title:"详细",
				path:"/report_concrete.html",
				moduleName:"reportConcrete",
				controllerName:"ReportConcreteController"
			},function($dialogScope)
			{
				service.discuss.report.findById(discuss.id,function(data)
				{
					$dialogScope.report=data;
					$dialogScope.$apply();
				});
			});
		};
		
		$scope.pre=function(discuss)
		{
			baseDialog
			({
				title:"预览",
				path:"/discuss_pre.html",
				moduleName:"discussPre",
				controllerName:"DiscussPreController"
			},function($preScope)
			{
				$preScope.discuss=discuss;
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//评论举报控制器
	controllers.controller("CommentReportShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.comment.report.find(pn,function(data)
			{
				$scope.comment=data;
				$scope.$apply();
			});
		};
		
		$scope.recover=function(comment)
		{
			baseRemove
			({
				func:service.comment.recoverDelete,
				$scope:$scope,
				item:comment,
				items:$scope.comment.data,
				message:"是否恢复?"
			});
		};
		
		$scope.remove=function(comment)
		{
			baseRemove
			({
				func:service.comment.remove,
				$scope:$scope,
				item:comment,
				items:$scope.comment.data
			});
		};
		
		$scope.concrete=function(comment)
		{
			baseDialog
			({
				title:"详细",
				path:"/report_concrete.html",
				moduleName:"reportConcrete",
				controllerName:"ReportConcreteController"
			},function($dialogScope)
			{
				service.comment.report.findById(comment.id,function(data)
				{
					$dialogScope.report=data;
					$dialogScope.$apply();
				});
			});
		};
		
		$scope.pre=function(comment)
		{
			baseDialog
			({
				title:"预览",
				path:"/comment_pre.html",
				moduleName:"commentPre",
				controllerName:"CommentPreController"
			},function($preScope)
			{
				$preScope.comment=comment;
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//讨论删除控制器
	controllers.controller("CommentDeleteShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.comment.findDelete(pn,function(data)
			{
				$scope.comment=data;
				$scope.$apply();
			});
		};
		
		$scope.recover=function(comment)
		{
			baseRemove
			({
				func:service.comment.recoverDelete,
				$scope:$scope,
				item:comment,
				items:$scope.comment.data,
				message:"是否恢复?"
			});
		};
		
		$scope.pre=function(comment)
		{
			baseDialog
			({
				title:"预览",
				path:"/comment_pre.html",
				moduleName:"commentPre",
				controllerName:"CommentPreController"
			},function($preScope)
			{
				$preScope.comment=comment;
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//显示用户控制器
	controllers.controller("UserShowController",["$scope",function($scope)
	{
		$scope.search={account:"",username:""};
		var find=function(pn)
		{
			var search=$scope.search;
			service.user.search(search.account,search.username,pn,function(data)
			{
				$scope.user=data;
				$scope.$apply();
			});
		};
		
		service.user.role.findType(function(data)
		{
			$scope.role=data;
			$scope.$apply();
		});
		
		$scope.updateRole=function(user,role)
		{
			core.ui.simpleDialogEx("是否更新?",function()
			{
				service.user.updateRole(user.id,role.id,function(result)
				{
					if(result.success)
					{
						user.role=role.id;
						$scope.$apply();
					}
					core.ui.simpleDialog(result.message);
				});
			});
		};
		
		$scope.kick=function(user)
		{
			core.ui.simpleDialogEx("是否踢下线?",function()
			{
				service.user.kick(user.id,function(result)
				{
					if(result.success)
					{
						user.online=false;
						$scope.$apply();
					}
					core.ui.simpleDialog(result.message);
				});
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//缓存节点控制器
	controllers.controller("CacheNodeShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.cache.node.find(pn,function(data)
			{
				$scope.nodes=data;
				$scope.$apply();
			});
		};
		
		$scope.create=function()
		{
			baseCreate
			({
				title:"创建缓存节点",
				path:"/cache_node_create.html",
				moduleName:"cacheNodeCreate",
				controllerName:"CacheNodeCreateController",
				controller:
				{
					func:service.cache.node.create,
					$scope:$scope,
					item:"node",
					items:$scope.nodes.data
				}
			});
		};
		
		$scope.edit=function(node)
		{
			baseEdit
			({
				title:"编辑节点",
				path:"/cache_node_edit.html",
				moduleName:"cacheNodeEdit",
				controllerName:"CacheNodeEditController",
				controller:
				{
					func:service.cache.node.update,
					$scope:$scope,
					source:node,
					item:"node",
					items:$scope.nodes.data
				}
			});
		};
		
		$scope.remove=function(node)
		{
			baseRemove
			({
				func:service.cache.node.remove,
				$scope:$scope,
				item:node,
				items:$scope.nodes.data
			});
		};
		
		find(1);
	}]);
	
	//自荐管理控制器
	controllers.controller("BlogRecommendShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.blog.recommend.find(pn,function(data)
			{
				$scope.recommends=data;
				$scope.$apply();
			});
		};
		
		$scope.pass=function(recommend)
		{
			baseRemove
			({
				message:"是否通过?",
				func:service.blog.recommend.pass,
				$scope:$scope,
				item:recommend,
				items:$scope.recommends.data
			});
		};
		
		$scope.refuse=function(recommend)
		{
			baseRemove
			({
				message:"是否拒绝?",
				func:service.blog.recommend.refuse,
				$scope:$scope,
				item:recommend,
				items:$scope.recommends.data
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
});